package com.maxsalad.pospal.service;

import com.maxsalad.common.service.GeneralService;
import com.maxsalad.common.util.PropertiesUtil;
import com.maxsalad.pospal.util.SignUtil;
import com.maxsalad.uc.em.Platform;
import com.maxsalad.uc.entity.ApiParameterEntity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-10.
 */
public abstract class BaseService extends GeneralService {
    protected String getBaseUrl(long shopId) throws Exception {
        ApiParameterEntity entity = getParameter(shopId);
        return entity.getBaseUrl();
    }

    protected String getAppId(long shopId) throws Exception {
        ApiParameterEntity entity = getParameter(shopId);
        return entity.getAppKey();
    }

    protected String getAppKey(long shopId) throws Exception {
        ApiParameterEntity entity = getParameter(shopId);
        return entity.getAppSecret();
    }

    protected String getSign(long shopId, String content) throws Exception {
        return SignUtil.getSign(content, getAppKey(shopId));
    }

    protected Map<String, String> getHeader(long shopId, String content) throws Exception {
        Map<String, String> header = new HashMap<String, String>();
        header.put("time-stamp", String.valueOf(System.currentTimeMillis()));//用毫秒
        header.put("data-signature", getSign(shopId, content));

        return header;
    }

    protected ApiParameterEntity getParameter(long shopId) throws Exception {
        List<ApiParameterEntity> list = getParameter(Platform.PosPal);
        if (list != null && list.size() > 0) {
            for (ApiParameterEntity entity : list) {
                if (entity.getShopId() == shopId) {
                    return entity;
                }
            }
        }
        throw new Exception("对应店铺的接口配置未找到");
    }

}
